import java.io.*;
import java.util.*;
import java.lang.*;

/**
* ScannerI
* A simple processing class to convert a
* text file into words understood by 
* ScannerII parser.
*/
public class ScannerI{

	/**
	* Constructor
	* @param fileName the file to be translated
	*/
	public ScannerI(String fileName){
		File file = new File(fileName);
		ArrayList input = scan(file);
		translate(input);
	}
	
	/**
	* Prints the input word by word one line at
	* a time numbered from 0. 
	*/
	public void print(ArrayList input){
		for(int i=0; i<input.size(); i++){
			System.out.print(i+":"+input.get(i).toString()+" ");
		}
	}
	
	/**
	* Reads in the given file into an arraylist
	* @return the given input word by word in an arraylist. 
	*/
	public ArrayList scan(File file){
		ArrayList contents = new ArrayList();
		BufferedReader reader = null;
	 
		try {
		   reader = new BufferedReader(new FileReader(file));
	 		String text = null;
	 
	      // repeat until all lines is read
			while ((text = reader.readLine()) != null) {
	          contents.add(text+"\n"); //add each line of input to an arraylist
	      }
	  	} catch (FileNotFoundException e) {
	      System.out.println("NO FILE");
	  	} catch (IOException e) {
	      e.printStackTrace();
	 	} finally {
	      try {
	          if (reader != null) {
	              reader.close();
	          }
	      } catch (IOException e) {
	          e.printStackTrace();
	      }
	  	}
		String[] tokens;
		ArrayList rValue= new ArrayList();
	 	for(int i=0; i< contents.size(); i++){
			tokens=contents.get(i).toString().split("[ ,\n]+"); //split each line into words in an array
			for(int j=0; j<tokens.length; j++){
				rValue.add((tokens[j])); //add each word to the return value arraylist
			}
		}		   
		return rValue;
  	}
	
	/**
	* Convert the given input into lexical classes
	* and writes the converted output into an output.txt file
	*
	* @param tokens an arraylist of words from the input file
	*/
	public void translate(ArrayList tokens){
		File file = new File("output.txt");
		try{
			BufferedWriter out = new BufferedWriter(new FileWriter(file));
			if(file.exists()){
				file.delete();
			}
			for(int i=0; i<tokens.size(); i++){ //for each word in the file
				
				//check if the next word is a keyword
				if(tokens.get(i).toString().equals("begin")){
					out.write("BEGIN ");
				}
				else if(tokens.get(i).toString().equals("end")){
					out.write("END ");
				}
				else if(tokens.get(i).toString().equals("print")){
					out.write("PRINT ");
				}
				else if(tokens.get(i).toString().equals("read")){
					out.write("READ ");
				}
				
				// check if the next word is an operator, left or right parenthesis,
				// statement delimitor or separator.
				else if(tokens.get(i).toString().equals(":=")){
					out.write("ASSIGN ");
				}
				else if(tokens.get(i).toString().equals("%")){
					out.write("MODULO ");
				}
				else if(tokens.get(i).toString().equals(",")){
					out.write("COMMA ");
				}
				else if(tokens.get(i).toString().equals("*")){
					out.write("MULTIPLY ");
				}
				else if(tokens.get(i).toString().equals("+")){
					out.write("PLUS ");
				}
				else if(tokens.get(i).toString().equals(";")){
					out.write("SEMICOLON ");
				}
				else if(tokens.get(i).toString().equals("(")){
					out.write("LEFTPAR ");
				}
				else if(tokens.get(i).toString().equals(")")){
					out.write("RIGHTPAR ");
				}
				else if(tokens.get(i).toString().equals("-")){
					out.write("MINUS ");
				}
				else{
					// if nothing else check to see if the first
					// character is a digit, corresponding to an INTNUM
					char[] charA = tokens.get(i).toString().toCharArray();
					if(charA[0]==('0')|| charA[0]==('1') || charA[0]==('2') || charA[0]==('3') || charA[0]==('4') || charA[0]==('5') || charA[0]==('6') || charA[0]==('7') || charA[0]==('8') || charA[0]==('9')){
						out.write("INTNUM ");
					}
					// or if the first character is a letter or underscore
					// it would be an ID
					else if(((((int)(charA[0]))>64) && (((int)(charA[0]))<91)) ||((((int)(charA[0]))>94)&&(((int)(charA[0]))<123))){
						out.write("ID ");
					}
					else{
					// if none of these match it is not a valid symbol
						System.out.println("Error!!");
						System.exit(0);
					}
				}
			}
			out.close();
		}
		catch (IOException e) {
		}			
	}
}